
import UIKit

class TF_LivingChatBar: UIView {

    @objc public let headView = UIView()
    @objc public var bottom = UIButton()
    /**发送*/
    @objc public let sendButton = UIButton()
    /**输入栏*/
    @objc public let chatBar = TF_TextView()
    /**表情*/
    @objc public let expression = TalkfunIncreaseButton()
    
    let backgroundView = UIView()
    let emoticonButtonArray = NSMutableArray()
    var KeyboardHeight:CGFloat = 0
    /*
     监听键盘的即将显示
     */
    @objc func keyboardWillShow(noti:NSNotification)
    {
  
           //不是聊天不用处理
        if (self.currentTextView(self.chatBar) == true) {
                
            }else{
                
                return;
                
            }
        
        let keyboardInfo:NSDictionary = noti.userInfo! as NSDictionary
        
        let keyboardFrameEnd:NSValue = keyboardInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
        
        let frame:CGRect = keyboardFrameEnd.cgRectValue
        self.KeyboardHeight = frame.size.height;
        
        
        let height:CGFloat = self.headView.frame.size.height;
        
        // 修改底部约束
         let  chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().size.height - frame.size.height - height
    
         let animationDuration:NSNumber = keyboardInfo.value(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! NSNumber
         //  执行动画
         let duration = animationDuration.floatValue
        UIView.animate(withDuration: TimeInterval(duration)) {
        
            self.frame = CGRect(x: 0, y: chatBarY, width:  self.frame.size.width, height: self.frame.size.height)
        }

        
    }
    /**
     *  监听键盘的即将隐藏
     */
    @objc func KeyboardWillHide(noti:NSNotification)
    {

            let keyboardInfo:NSDictionary = noti.userInfo! as NSDictionary
            let animationDuration:NSNumber = keyboardInfo.value(forKey: UIResponder.keyboardAnimationDurationUserInfoKey) as! NSNumber
            //  执行动画
            let duration = animationDuration.floatValue
            
        self.keyboardWillHideUI(CGFloat(duration))
        
//            let  chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().size.height
//
//            UIView.animate(withDuration: TimeInterval(duration)) {
//
//               self.frame = CGRect(x: 0, y: chatBarY, width:  self.frame.size.width, height: self.frame.size.height)
//            }
//
    }
    
    func keyboardWillHideUI(_ duration:CGFloat)
    
    {
        let  chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().size.height
        
        UIView.animate(withDuration: TimeInterval(duration)) {
          
           self.frame = CGRect(x: 0, y: chatBarY, width:  self.frame.size.width, height: self.frame.size.height)
        }
    }
        
    @objc public func destroy()
    {
        NotificationCenter.default.removeObserver(self)
    }
    @objc func bottomClick()
    {
        
    }
    @objc func emoticonClick(btn:TF_LivingButton)
    {
        
        let text:String = self.chatBar.text + btn.emoticonName
        
        self.chatBar.text = text
        
        self.chatBar.setNeedsDisplay()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //键盘显示前
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(noti:)), name: UIResponder.keyboardWillShowNotification, object: nil)
      
        //键盘隐藏前
        NotificationCenter.default.addObserver(self, selector: #selector(self.KeyboardWillHide(noti:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        
        self.backgroundColor = UIColor.clear


        self.headView.backgroundColor = UIColor(red: 246/255.0, green: 248/255.0, blue: 249/255.0, alpha: 1)
        self.addSubview(self.headView)
            
        self.bottom.backgroundColor =  UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1)
        self.addSubview(self.bottom)
        self.bottom.addTarget(self, action: #selector(bottomClick), for: .touchUpInside)
        
      
        let  ar1:NSArray =   ["[slime]",
                              "[laugh]",
                              "[love]",
                              "[tongue]",
                              "[daze]",
                              "[handclap]",
                              "[titter]",
                              "[handssors]",
                              "[six]",
                              "[heart]",
                              "[melon]",
                              "[silly]",
                              "[dog]",
                              "[like]",
                              "[expect]",
                              "[flower]"]
            
        let  ar2:NSArray =  ["slime",
                              "laugh",
                              "love",
                              "tongue",
                              "daze",
                              "handclap",
                              "titter",
                              "handssors",
                              "six",
                              "heart",
                              "melon",
                              "silly",
                              "dog",
                              "like",
                              "expect",
                              "flower"
                              
                   ]
           
        for i in stride(from: 0, to: ar1.count, by: 1)
        {

           let btn = TF_LivingButton()
            btn.emoticonName = (ar1[i] as! NSString) as String
            btn.tag = i+1
            let imageName = (ar2[i] as! NSString) as String
            btn.setImage(UIImage.init(named: imageName), for: .normal)
            btn.addTarget(self, action: #selector(emoticonClick), for: .touchUpInside)
            self.bottom.addSubview(btn)
            self.emoticonButtonArray.add(btn)
        }
            
        self.headView.addSubview(self.sendButton)
        self.sendButton.setTitle("发送", for: .normal)
        self.sendButton.backgroundColor = UIColor(red: 1/255.0, green: 194/255.0, blue: 255/255.0, alpha: 1)
        self.sendButton.layer.cornerRadius = 17.5
        
      self.headView.addSubview(self.backgroundView)
        
       self.chatBar.font = UIFont.PingFang(size: 16)
       self.chatBar.tf_placeholder = "说点什么吧"
        
        self.backgroundView.addSubview(self.chatBar)
            

        self.backgroundView.addSubview(self.expression)
        self.expression.setImage(UIImage.init(named: "HTLive.bundle/LivingExpression"), for: .normal)
        self.expression.setImage(UIImage.init(named: "HTLive.bundle/LivingWord"), for: .selected)
        self.expression.addTarget(self, action: #selector(expressionClick), for: .touchUpInside)
       
    }
    //切换为表情输入
    @objc func expressionClick(btn:TalkfunIncreaseButton)
    {

         if(self.bottom.alpha==0)
            {
             self.bottom.alpha = 1
             self.chatBar.resignFirstResponder()
             let chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().height - self.frame.size.height;
             UIView.animate(withDuration: 0.5) {
                 self.frame = CGRect(x: 0, y: chatBarY, width: self.frame.size.width, height: self.frame.size.height)
             }
            
            
         }else{
             //成为第一响应
             self.chatBar.becomeFirstResponder()
             self.bottom.alpha = 0
         }

    }
    
    func  bottomHidde()
    {
        self.bottom.alpha = 0
        let chatBarY:CGFloat = TalkfunSharedTools.getMainScreen().height
        UIView.animate(withDuration: 0.5) {
            self.frame = CGRect(x: 0, y: chatBarY, width: self.frame.size.width, height: self.frame.size.height)
        }
    }
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01) {
            return nil
        }
     
        let hitView = super.hitTest(point, with: event)
        // 用pointInside方法判断点在不在控件上,point这个必须是方法调用者坐标系上的点
        //self.point(inside: point, with: event) == false
//        if ([self pointInside:point withEvent:event] == NO) return nil;
        
        if(hitView == self){

        return nil

        }
        //已经显示表情了
        if self.bottom.alpha == 1 && self.point(inside: point, with: event) == false {
         
            self.bottomHidde()
        }

        return hitView

    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.headView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 50)

        
        self.sendButton.frame = CGRect(x: self.headView.frame.size.width - 55.5 - 12, y: (self.headView.frame.size.height - 35)/2, width: 55.5, height: 35)
        
        
         let backgroundViewW = self.sendButton.frame.origin.x - 17.5 - 21
        
        self.backgroundView.frame = CGRect(x: 21, y: (self.headView.frame.size.height - 35)/2, width: backgroundViewW, height: 35)
        
        self.backgroundView.layer.borderColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1).cgColor
        
         self.backgroundView.layer.borderWidth = 0.5;
         self.backgroundView.backgroundColor = UIColor(red: 255/255.0 , green: 255/255.0 , blue: 255/255.0 , alpha: 1)
   
         self.backgroundView.layer.cornerRadius = 17.5
        
        self.chatBar.frame = CGRect(x: 16, y: 0, width: self.backgroundView.frame.size.width - 16 - 23 - 9.5, height: self.backgroundView.frame.size.height)
        
        
        self.expression.frame = CGRect(x: self.backgroundView.frame.size.width - 23 - 9.5, y: (self.backgroundView.frame.size.height - 23)/2, width: 23, height:23 )
        
        self.bottom.frame = CGRect(x: 0, y: self.frame.size.height - 203, width: self.frame.size.width, height: 203)
        

        
        // 一共有多少列
        let COL_COUNT = 8;
        let PIC_WIDTH = 30;
        let PIC_HEIGHT = 30;

        //总个数
        let count = self.emoticonButtonArray.count
        
        for i in stride(from: 0, to: count, by: 1)
        {

            let btn = self.emoticonButtonArray[i] as! UIButton
            
            // 图片所在行
            let row = i / COL_COUNT
            // 图片所在列
            let col = i % COL_COUNT
            // 间距
            let margin = (NSInteger(self.bottom.bounds.size.width) - (PIC_WIDTH * COL_COUNT)) / (COL_COUNT + 1);
            
            // PointX
            let picX = margin + (PIC_WIDTH + margin) * col;
            // PointY
            let picY = margin + (PIC_HEIGHT + margin) * row;
          
            btn.frame = CGRect(x: picX, y: picY, width: PIC_WIDTH, height: PIC_HEIGHT)
            

        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   

}
